import {
  Module,
  MiddlewareConsumer,
  NestModule,
  Logger,
  Global,
} from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { AppController } from './app.controller'
import { UserModule } from './user/user.module'
import { OrderModule } from './order/order.module'
import { LoginValidateMiddleware } from './login-validate/login-validate.middleware'
import { LogModule } from './common/log/log.module'
import { AppRedisModule } from './redis/redis.module'
import { PrismaModule } from './prisma/prisma.module'
import { AuthModule } from './auth/auth.module'
import { APP_GUARD } from '@nestjs/core'
// import { AuthGuard } from '@nestjs/passport'
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'

@Global()
@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    PrismaModule,
    UserModule,
    OrderModule,
    LogModule,
    AppRedisModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [
    Logger,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
  ],
  exports: [Logger],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoginValidateMiddleware).forRoutes(...['order'])
  }
}
